package com.dyh.quickdemo.spel;

import com.alibaba.fastjson.JSONObject;
import com.dyh.quickdemo.injectMap.BizHandlerHQ;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/*
@Author=Joey Wheeler
@Date=2021/5/19 11:14
*/
@SpringBootTest
public class TestSpel {

    @Autowired
    BizHandlerHQ bizHandlerHQ;

    @Test
    public void test(){
        SpelExpressionParser spelParser = new SpelExpressionParser();
        EvaluationContext context = new StandardEvaluationContext();

        bizHandlerHQ.getHandlerMap().forEach(context::setVariable);
        /*
        等价于
        (k,v)->{
            context.setVariable(k, v);
        }
        */

        JSONObject jo = new JSONObject();
        jo.put("name","yjsnp");
        jo.put("age",22);

        context.setVariable("jo",jo);

        Expression expression = spelParser.parseExpression(
                "#type97SendBizHandler.handle(1,'hello',#jo)"
        );

        Object value = expression.getValue(context);

        System.out.println();
    }


}
